package cn.thornbird.orgsync.model.feishu;

import cn.thornbird.orgsync.model.AppUserData;
import cn.thornbird.orgsync.util.JsonUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Map;

@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class UserInfo extends UserList.User {

    public static final String CREATE_FIELDS = "name,mobile,department_ids,employee_type,user_id,en_name,nickname," +
            "email,mobile_visible,gender,avatar_key,leader_user_id,city,country,work_station,join_time,employee_no," +
            "orders,custom_attrs,enterprise_email,job_title,geo,job_level_id,job_family_id,subscription_ids," +
            "dotted_line_leader_user_ids";

    public static final String UPDATE_FIELDS = "user_id,name,en_name,nickname,email,mobile,mobile_visible,gender," +
            "avatar_key,department_ids,leader_user_id,city,country,work_station,join_time,employee_no,employee_type," +
            "orders,custom_attrs,enterprise_email,job_title,is_frozen,job_level_id,job_family_id,subscription_ids," +
            "dotted_line_leader_user_ids";

    public Map<String, Object> toCreateParams() {
        return toParams(CREATE_FIELDS);
    }

    public Map<String, Object> toUpdateParams() {
        return toParams(UPDATE_FIELDS);
    }

    public Map<String, Object> toParams(String fields) {
        return JsonUtil.toMap(this, StringUtils.isEmpty(fields) ? null : fields.split(","));
    }

    public UserInfo setId(String id) {
        setUserId(id);
        return this;
    }

    public UserInfo setDepartments(Map<String, AppUserData.DepartmentInfo> departments, String mainDepartmentId) {
        if (departments != null && !departments.isEmpty()) {
            setDepartmentIds(new ArrayList<>());
            setOrders(new ArrayList<>());
            for (Map.Entry<String, AppUserData.DepartmentInfo> entry: departments.entrySet()) {
                String id = entry.getKey();
                UserList.UserOrder order = new UserList.UserOrder();
                order.setDepartmentId(id);
                order.setUserOrder(entry.getValue().getOrder() == null ? 0 : entry.getValue().getOrder().intValue());
                order.setIsPrimaryDepartment(mainDepartmentId != null && mainDepartmentId.equals(id));
                if (order.getIsPrimaryDepartment()) {
                    getOrders().add(0, order);
                    getDepartmentIds().add(0, id);
                } else {
                    getOrders().add(order);
                    getDepartmentIds().add(id);
                }
            }
        }
        return this;
    }

    public UserInfo setLeaderId(String leaderId) {
        if (!StringUtils.isEmpty(leaderId)) {
            setLeaderUserId(leaderId);
        } else {
            setLeaderUserId(null);
        }
        return this;
    }

    public UserInfo setDefaultEmployeeType() {
        setEmployeeType(1);
        return this;
    }

}
